<?
$time_start = microtime(true);
include_once '/var/www/html/new/com.inc.php';
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s= new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$array = include '/var/www/html/new/d/module/zj_terminal/zj_permissions.php';

logfile(date('Y-m-d H:i:s')."--".json_encode($_REQUEST));

$wac=$_REQUEST['ac'];
if(!in_array($wac,$array)){
	echo '{"code":"1011","msg":"无授权，拒绝连接"}';
	exit;
}

//根据账号生成接口编码
$sql="select dname,id from pft_member where account='$wac' limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
$dname=$GLOBALS['le']->f('dname');
$aid=$GLOBALS['le']->f('id');
$wpw=$_REQUEST['pw'];
$chpw=$s->Create_Conn_REC($wac,$dname);
//查看该订单是不是该供应商景区的订单，是该景区的订单才能验证
$sid=$_REQUEST['sid'];
$sel="select apply_did from uu_land where salerid='$sid' limit 1";
$GLOBALS['le']->query($sel);
$GLOBALS['le']->fetch_assoc();
$apply_did=$GLOBALS['le']->f('apply_did');
$code=$_REQUEST['code'];
if($aid!=$apply_did){
	echo '{"code":"4002","msg":"非自供订单，不能验证"}';
	exit;
}elseif($wpw!=$chpw){
	echo '{"code":"101","msg":"无权限，拒绝连接"}';
	exit;
}else{
	if($code!=""){//根据凭证码验证
		$time=date("Y-m-d H:i:s");
		$r=$s->Terminal_Check_In($sid,$code,null,"");
		if(is_numeric($r)){
			$code = $r;
			logfile($r);
			$repMsg = Err_Word($r);
			$response = '{"code":"'.$r.'","msg":"'.$repMsg.'"}';
			echo $response;
			$time_end = microtime(true) - $time_start;
			logfile($time_end);
			exit;
		}
		else{
				$obj=@simplexml_load_string($r);
				$ordernum=(string)$obj->Rec->UUordernum;
				$o=$s->Order_Globle_Search(
											$sid, "", "", "", "",
											"", "", "", "", "",
											"", "", $ordernum, "", "", 
											"", "", "", 1, 1, 
											0, 1, 0, 0, "",
											0, "", 0, '', '', 
											"", 0, "",	"", "", "" 	
										); 
				$os=simplexml_load_string($o);
				$UUaids=(string)$os->Rec->UUaids;
				if($UUaids == 0){
					$buyid = (string)$os->Rec->UUbuyid;
				}else{
					$UUaidss= explode(',',$UUaids);
					$buyid = $UUaidss[1];
				}	
				logfile($ordernum."---".$buyid);
				$sql="select dname from pft_member where id=".$buyid." limit 1";
				$GLOBALS['le']->query($sql);
				$GLOBALS['le']->fetch_assoc(); 
				$fendname=$GLOBALS['le']->f('dname');
				$response = '{"code":"100","msg":"验证成功","time":"'.$time.'","fenxiaoshang":"'.$fendname.'"}';
				echo $response;
				$time_end = microtime(true) - $time_start;
				logfile($time_end);
				exit;
		}
	}

}

function Err_Word($r){
	$errCode = array(
		'105'=>'找不到订单',
		'121'=>'订单状态出错,订单已验证',
		'119'=>'订单状态出错,已取消或其他状态',
		'521'=>'验证间隔时间未到',
		'133'=>'游玩时间未到',
		'134'=>'游玩时间已过',
		'118'=>'终端服务器无响应',
		'1070'=>'订单未支付',
	);
	$eword=$errCode[$r];
	if (!$eword) $eword='其他错误';
	return $eword;
}

function logfile($txt){
		$fp = fopen('/var/www/html/new/d/module/zj_terminal/log.txt',"a");
		flock($fp, LOCK_EX) ;
		fwrite($fp,$txt."\n");
		flock($fp, LOCK_UN);
		fclose($fp); 
}
?>